﻿Imports EstoqueDAL
Imports EstoqueBLL
Public Class ComprasFRM
    Dim objforn As New FornecedorBLL
    Private Sub Buscar_Click(sender As System.Object, e As System.EventArgs) Handles bbuscar2.Click
        If rbfornecedor.Checked Then
            If (Not IsNothing(objforn)) Then
                If (objforn.Codigo > 0) Then
                    gerarelatrio("Relatório de Compras de " & objforn.razao, "{Ofi_Compras.for_codigo}='" & objforn.Codigo & "'")
                End If
            End If
        End If
    End Sub

    Private Sub bbuscar2_Click(sender As System.Object, e As System.EventArgs) Handles Buscar.Click
        Try
            If (dtinicio.Value.Date > dtfim.Value.Date) Then Throw New Exception("Data início não pode ser maior que data de fim")
            If rbemissao.Checked Then gerarelatrio("Relatório de Compras: " & dtinicio.Value.Date & " até " & dtfim.Value.Date, "{Ofi_Compras.com_dtemissao} >= DATETIME('" & dtinicio.Value.ToString("yyyy-MM-dd") & " 00:00:00') and {Ofi_Compras.com_dtemissao}<= DATETIME('" & dtfim.Value.ToString("yyyy-MM-dd") & " 23:59:59')")
            If rbchegada.Checked Then gerarelatrio("Relatório de Compras:" & dtinicio.Value.Date & " até " & dtfim.Value.Date, "{Ofi_Compras.com_dtchegada} >= DATETIME('" & dtinicio.Value.ToString("yyyy-MM-dd") & " 00:00:00') and {Ofi_Compras.com_dtchegada}<= DATETIME('" & dtfim.Value.ToString("yyyy-MM-dd") & " 23:59:59')")

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
        
    End Sub

    Private Sub rbfornecedor_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rbfornecedor.CheckedChanged
        gbf.Enabled = True
        gbp.Enabled = False
        chamar()
    End Sub

    Private Sub rbemissao_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rbemissao.CheckedChanged
        gbf.Enabled = False
        gbp.Enabled = True
    End Sub

    Private Sub rbchegada_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rbchegada.CheckedChanged
        gbf.Enabled = False
        gbp.Enabled = True
    End Sub

    Private Sub rbfechado_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rbfechado.CheckedChanged
        gbf.Enabled = False
        gbp.Enabled = False
        If rbfechado.Checked Then gerarelatrio("Relatório de Compras Fechadas", "{Ofi_Compras.sta_codigo}=6")
    End Sub

    Private Sub rbaberto_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rbaberto.CheckedChanged
        gbf.Enabled = False
        gbp.Enabled = False
        If rbaberto.Checked Then gerarelatrio("Relatório de Compras Abertas", "{Ofi_Compras.sta_codigo}=4")
    End Sub

    Private Sub BLocaliza_Click(sender As System.Object, e As System.EventArgs) Handles BLocaliza.Click
        chamar()
    End Sub

#Region "Relatorio"
    Public Sub gerarelatrio(titulo As String, formula As String)
        Try
            Dim frmrecursos As New RelatoriosFRM
            Dim rpt As New ComprasGeral

            Try
                rpt.SetDatabaseLogon(usuarios, senha)
                rpt.SetParameterValue(0, titulo)
                rpt.RecordSelectionFormula = formula
                frmrecursos.CrystalReportViewer1.ShowGroupTreeButton = False
                frmrecursos.CrystalReportViewer1.ReportSource = rpt
                frmrecursos.ShowDialog()
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
            frmrecursos.Dispose()
            rpt = Nothing
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Finally

        End Try
    End Sub
#End Region

#Region "Chamar Fornecedor"
    Public Sub chamar()
        If (rbfornecedor.Checked) Then
            Dim obj As New FornecedorCON
            obj.tipo = "ret"
            obj.ShowDialog()
            If (Not IsNothing(obj.objforn)) Then
                objforn = obj.objforn
                TextBox1.Text = objforn.razao
            End If
        End If
    End Sub
#End Region
End Class